package com.suntop.carousel.qlmediaplay.video.control;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;


import com.suntop.carousel.R;
import com.suntop.carousel.qlmediaplay.field.Field_State;
import com.suntop.carousel.qlmediaplay.tools.PlayerUtils;
import com.suntop.carousel.qlmediaplay.video.player.IMediaPlayerControl;
import com.suntop.carousel.qlmediaplay.video.player.IVideoPlayer;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class LiteControl extends FrameLayout implements IMediaPlayerControl
        , View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private TextView startSeek;
    private TextView endSeek;
    private SeekBar seekBar;
    private TextView statePlay;
    private IVideoPlayer iVideoPlayer;
    private static int sDefaultTimeout = 5000;
    private int postDelayTime = 0;
    private static final int SEEK_TO_POST_DELAY_MILLIS = 200;
    private boolean  isFromUse;

    public LiteControl(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public LiteControl(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LiteControl(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View inflate = LayoutInflater.from(context)
                .inflate(R.layout.control_lite_layout, this, false);
        startSeek = inflate.findViewById(R.id.tx_start_seek);
        endSeek = inflate.findViewById(R.id.tx_end_video);
        seekBar = inflate.findViewById(R.id.seek_video);
        seekBar.setOnSeekBarChangeListener(this);
        statePlay = inflate.findViewById(R.id.play_state);
        statePlay.setOnClickListener(this);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
                , 60);
        this.addView(inflate, layoutParams);
    }

    @Override
    public void setMediaPlayer(IVideoPlayer var1) {
        iVideoPlayer = var1;
    }

    @Override
    public void setSeekBar(int seekBar) {
        if (this.seekBar != null) {
            this.seekBar.setProgress(seekBar);
        }
        startSeek.post(new Runnable() {
            @Override
            public void run() {
                startSeek.setText(PlayerUtils.stringForTime(seekBar));
            }
        });
    }

    @Override
    public void setDuration(int duration) {
        if (this.seekBar != null) {
            this.seekBar.setMax(duration);
        }
        endSeek.post(new Runnable() {
            @Override
            public void run() {
                endSeek.setText(PlayerUtils.stringForTime(duration));
            }
        });
    }

    @Override
    public void hid() {
        this.setVisibility(GONE);
        removeCallbacks(runnable);
    }

    @Override
    public void show() {
        this.setVisibility(VISIBLE);
        if (iVideoPlayer != null && iVideoPlayer.isOnPlaying()) {
            statePlay.setText("暂停");
        } else {
            statePlay.setText("播放");
        }
        postDelayTime = 0;
        removeCallbacks(runnable);
        postDelayed(runnable, SEEK_TO_POST_DELAY_MILLIS);
    }

    @Override
    public boolean isShow() {
        return getVisibility() == VISIBLE;
    }

    @Override
    public void release() {
        removeCallbacks(runnable);
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onClick(View v) {
        if (iVideoPlayer != null && iVideoPlayer.isOnPlaying()
                && iVideoPlayer.getPlayState() == Field_State.PLAYING) {
            statePlay.setText("播放");
            iVideoPlayer.pause();
        } else {
            statePlay.setText("暂停");
            iVideoPlayer.start();
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        isFromUse=fromUser;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        LiteControl.this.removeCallbacks(runnable);
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (iVideoPlayer != null&&isFromUse) {
            int progress = seekBar.getProgress();
            iVideoPlayer.seekTo(progress);
            setSeekBar(progress);
        }
        show();
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (iVideoPlayer != null&&View.VISIBLE==getVisibility()) {
                long currentPosition = iVideoPlayer.getCurrentPosition();
                setSeekBar((int) currentPosition);
                postDelayTime = postDelayTime + SEEK_TO_POST_DELAY_MILLIS;
                if (postDelayTime < sDefaultTimeout) {
                    postDelayed(this, SEEK_TO_POST_DELAY_MILLIS);
                } else {
                    hid();
                }
            }

        }
    };
}
